<?php
/*
    Lightweight and multiplatform PHP Multithreading Engine
    Copyright (C) 2008  Anton Vedeshin (anton@vedeshin.com, http://anton.vedeshin.com)
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

error_reporting(E_ALL);

require_once("functions.php");
//file that contains caclulation function
//this is the only thing you should change
require_once("calculate.php");

//### DATABASE SETTINGS
define ("s_host","yourdbhost");
define ("s_login","yourlogin");
define ("s_password","yourpassword");
define ("s_db_name","yourdbname");
define ("s_db_name_threads","threads");

//### DATABASE INCLUDES AND CONNECTION
// Include ezSQL core
include_once "x:/your_path_to_ezSQL/db/shared/ez_sql_core.php";
// Include ezSQL database specific component
include_once "x:/your_path_to_ezSQL/db/mssql/ez_sql_mssql.php";

$proc_id=$argv[1];
$l=new logger($proc_id);
$l->dolog(true);
echo "thread: ".$proc_id;
$l->lw(1,"All is included");

$db = new ezSQL_mssql(s_login, s_password, s_db_name_threads, s_host);
$l->lw(1,"DB is connected");

$result=$db->query("UPDATE threads SET state=1 WHERE proc_id=".$proc_id);
$l->lw(1,"State ready is set! rows: ".$result);

$nowork=0;
$results=null;
do{
	//get work
	$qry_results=$db->get_results("SELECT TOP 1 cmd_id, cmd, param FROM cmd WHERE done=0 AND proc_id=$proc_id ORDER BY datestamp DESC");

	if(count($qry_results)>0){
		$l->lw(1,"Command lines:".count($qry_results));
		foreach ($qry_results as $results){
			$l->lw(1,"Got command: ".$results->cmd);

			switch ($results->cmd) {
				case "calculate":
					$l->lw(1,"inside: ".$results->cmd);
					$query="UPDATE threads SET last_beat=".(time()+60)." WHERE proc_id=$proc_id;";
					$affected=$db->query($query);
					$l->lw(1,"set last beat, affected: ".$affected);

					$state=$db->query("UPDATE threads SET busy=1 WHERE proc_id=".$proc_id);
					$l->lw(1,"State busy is set! rows: ".$state);

					//calculation
					$l->lw(1,"cmd_id=".$results->cmd_id." \n");
					
					$param=unserialize(base64_decode($results->param));
					///计算
					$return_param=calculate($c,$param,$proc_id);

					//store results					
					$db = new ezSQL_mssql(s_login, s_password, s_db_name_threads, s_host);
					$state=$db->query("UPDATE cmd SET done=1, result='".base64_encode(serialize($return_param))."' WHERE cmd_id=".$results->cmd_id);
					$l->lw(1,"Result saved rows:".$state);
					
					//sometimes connection is broken, is it MSSQL issue, or php bug or some other problem
					//but we just reestablish it, remove it if everything is working with out this line
					$db = new ezSQL_mssql(s_login, s_password, s_db_name_threads, s_host); 
					$state=$db->query("UPDATE threads SET busy=0 WHERE proc_id=".$proc_id);
					$l->lw(1,"State busy->ready is set! rows: ".$state);
					break;

				case "exit":
					$db = new ezSQL_mssql(s_login, s_password, s_db_name_threads, s_host);
					$state=$db->query("UPDATE threads SET state=3 WHERE proc_id=".$proc_id);
					$l->lw(1,"State closed is set! rows: ".$state);
					exit();
					break;

				default:
					break;
			}
		}
	}else{
		if($nowork>5){
			$db = new ezSQL_mssql(s_login, s_password, s_db_name_threads, s_host);
			$state=$db->query("UPDATE threads SET state=3 WHERE proc_id=".$proc_id);
			$l->lw(1,"There is no work :(, terminating, rows: ".$state);
			exit;
		}
		$nowork++;
		$l->lw(1,"There is no work yet!");
		Sleeper(10000);
	}
}while(true);

?>



